// 125.[栈DFS中序遍历] Expression 表达式计算
// https://oj.rnd.huawei.com/problems/125/details
//给你一个数学表达式，请你求出它的值。
//表达式中可能含有以下运算符：”+”、”-“、”*“、”/“、”%”(取余)、”^”(指数)、”(“、”)”，
//其中优先级为: 括号 > “^” > “*” = “/“ = “%” > “+” = “-“。
// 输入 ( 2 + 1 ) * 3 $
// 输出 9

#include <math.h>

#include <iostream>
#include <map>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

int eval(stack<int>& stNum, stack<char>& stOper) {
  auto b = stNum.top();
  stNum.pop();
  auto a = stNum.top();
  stNum.pop();
  auto op = stOper.top();
  stOper.pop();
  int x;
  if (op == '+')
    x = a + b;
  else if (op == '-')
    x = a - b;
  else if (op == '*')
    x = a * b;
  else if (op == '/')
    x = a / b;
  else if (op == '%')
    x = a % b;
  else if (op == '^')
    x = pow(a, b);
  stNum.push(x);
  return 0;
}

int calculateExpress() {
  char c;
  map<char, int> prior{{'+', 1}, {'-', 1}, {'*', 2},
                       {'/', 2}, {'%', 2}, {'^', 3}};
  stack<int> stNumber;
  stack<char> stOper;
  while (cin >> c) {
    if (c == '$') break;
    if (isdigit(c)) {
      stNumber.push(c - '0');
    } else if (c == '(') {
      stOper.push(c);
    } else if (c == ')') {
      while (stOper.top() != '(') {
        eval(stNumber, stOper);
      }
      stOper.pop();
    } else {  // is oper
      // 优先级大进栈,
      // 优先级小或相等,先把栈内符号出栈运算
      while (stOper.size() && prior[c] <= prior[stOper.top()])
        eval(stNumber, stOper);
      stOper.push(c);
    }
  }
  while (stOper.size()) eval(stNumber, stOper);
  cout << stNumber.top();
  return 0;
}

int main_125() {
  calculateExpress();
  return 0;
}
